package rank1400;

import _object.TreeNode;

import java.util.*;

/**
 * 2415. 反转二叉树的奇数层
 */
public class No2415 {
    public TreeNode reverseOddLevels(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        boolean reverse = false;
        while (!queue.isEmpty()) {
            List<TreeNode> curList = new ArrayList<>();
            int size = queue.size();
            for (int j = 0; j < size; j++) {
                TreeNode poll = queue.poll();
                curList.add(poll);
                if (poll.left != null) queue.offer(poll.left);
                if (poll.right != null) queue.offer(poll.right);
            }
            if (reverse) {
                for (int j = 0; j < size / 2; j++) {
                    int temp = curList.get(j).val;
                    curList.get(j).val = curList.get(size - j - 1).val;
                    curList.get(size - j - 1).val = temp;
                }
            }
            reverse = !reverse;
            curList.clear();
        }
        return root;
    }
}
